package com.tcoiss.datafactory.controller;

import com.tcoiss.common.core.utils.poi.ExcelUtil;
import com.tcoiss.common.core.web.controller.BaseController;
import com.tcoiss.common.core.web.domain.AjaxResult;
import com.tcoiss.common.core.web.page.TableDataInfo;
import com.tcoiss.common.log.annotation.Log;
import com.tcoiss.common.log.enums.BusinessType;
import com.tcoiss.common.security.annotation.PreAuthorize;
import com.tcoiss.datafactory.domain.KJob;
import com.tcoiss.datafactory.service.IKJobService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 作业Controller
 * 
 * @author zw
 * @date 2021-06-01
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/job" )
public class KJobController extends BaseController {

    private final IKJobService iKJobService;

    /**
     * 查询作业列表
     */
    @PreAuthorize(hasPermi = "datafactory:job:list")
    @GetMapping("/list")
    public TableDataInfo list(KJob kJob) {
        startPage();
        List<KJob> list = iKJobService.queryList(kJob);
        return getDataTable(list);
    }

    /**
     * 启动ETL作业
     */
    @GetMapping(value = "/startJob.do/{id}" )
    public AjaxResult startTrans(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iKJobService.startJobById(id));
    }

    /**
     * 查询作业列表
     */
    @PreAuthorize(hasPermi = "datafactory:job:list")
    @GetMapping("/getJob")
    public List<KJob> getJobList(KJob kJob) {
        return iKJobService.queryList(kJob);
    }

    /**
     * 导出作业列表
     */
    @PreAuthorize(hasPermi = "datafactory:job:export" )
    @Log(title = "作业" , businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,KJob kJob) throws IOException
    {
        List<KJob> list = iKJobService.queryList(kJob);
        ExcelUtil<KJob> util = new ExcelUtil<KJob>(KJob.class);
        util.exportExcel(response,list, "job" );
    }

    /**
     * 获取作业详细信息
     */
    @PreAuthorize(hasPermi = "datafactory:job:query" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iKJobService.getById(id));
    }

    /**
     * 新增作业
     */
    @PreAuthorize(hasPermi = "datafactory:job:add" )
    @Log(title = "作业" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody KJob kJob) {
        return toAjax(iKJobService.save(kJob) ? 1 : 0);
    }

    /**
     * 修改作业
     */
    @PreAuthorize(hasPermi = "datafactory:job:edit" )
    @Log(title = "作业" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody KJob kJob) {
        return toAjax(iKJobService.updateById(kJob) ? 1 : 0);
    }

    /**
     * 删除作业
     */
    @PreAuthorize(hasPermi = "datafactory:job:remove" )
    @Log(title = "作业" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}" )
    public AjaxResult remove(@PathVariable Long[] id) {
        return toAjax(iKJobService.removeByIds(Arrays.asList(id)) ? 1 : 0);
    }
}
